﻿package com.eaglesandcobras.games.scenes {
	import com.gaiaframework.templates.AbstractPage;
	import com.gaiaframework.api.Gaia;
	import com.eaglesandcobras.games.scenes.Pages;
	import flash.display.MovieClip;
	import flash.events.*;
	import flash.utils.Timer;
	import flash.utils.getDefinitionByName;
	
	public class GamePage extends AbstractPage{
		private var _params:Object = new Object();
		public var _content:Content;
		private var _button:MovieClip;
		private var _functions:Array;
		private var _current:int;
		
		public function GamePage() {
			super();
			this.visible = false;
			
			if (content) content.visible = false;
		}
		
		/*********************************
		INIT
		**********************************/
		public function init():void {
			
		}
		
		/*********************************
		CONTENT
		**********************************/
		public function get content():MovieClip {
			return _content;
		}
		
		/*********************************
		TRANSITION IN / OUT
		**********************************/
		override public function transitionIn():void 
		{
			super.transitionIn();
			this.visible = true;
			transitionInComplete();
			init();
			if ( content) content.visible = true;
		}
		override public function transitionOut():void 
		{
			super.transitionOut();
			this.visible = false;
			transitionOutComplete();
		}
		
		/*********************************
		SCENE DONE - PASS / FALL
		**********************************/
		public function sceneDone($b:Boolean):void {
			cleanup();
			this.dispatchEvent( new Event( Event.COMPLETE ) );
			
			if ($b) {
				Gaia.api.getPage( Pages.BASE ).content.passLevel();
			} else {
				Gaia.api.getPage( Pages.BASE ).content.gameOver();
			}
		}
		
		/*********************************
		WIN / LOSE
		**********************************/
		public function loseLevel(e:Event=null):void {
			sceneDone(false);
		}
		
		public function winLevel (e:Event=null):void {
			sceneDone(true);
		}
		
		public function get winGame():Boolean {
			return Gaia.api.getPage( Pages.BASE ).content.win;
		}
		
		/*********************************
		BUTTON
		**********************************/
		public function set button($m:MovieClip):void {
			$m.buttonMode = true;
			$m.mouseChildren = false;
			
			$m.addEventListener(MouseEvent.CLICK, buttonClicked);
		}
		
		private function buttonClicked(e:MouseEvent):void {
			sceneDone(true);
		}
		
		/*********************************
		Cleanup
		**********************************/
		public function cleanup():void {
			//OVERRIDE THIS
		}
		
		/*********************************
		PARAM
		**********************************/
		public function get params():Object {
			return Gaia.api.getPage( Pages.BASE ).content.getParams();
		}
		
		/*********************************
		PLAY SOUND
		**********************************/
		public function playSound($name:String, $volume:Number=.5):void {
			Gaia.api.getPage( Pages.BASE ).content.playGameSound($name,$volume);
		}
		
		public function stopSound($name:String):void {
			Gaia.api.getPage( Pages.BASE ).content.stopGameSound($name);
		}
		
		/*********************************
		GLOBAL SOUND 
		**********************************/
		public function switchGlobalSound():Boolean {
			Gaia.api.getPage( Pages.BASE ).content.switchSound();
			return Gaia.api.getPage( Pages.BASE ).content.globalSound;
		}
		
		public function get soundOn():Boolean {
			return Gaia.api.getPage( Pages.BASE ).content.globalSound;
		}
		
		/*********************************
		SOUND UPDATE
		**********************************/
		public function addSoundUpdate($function:Function):void {
			Gaia.api.getPage( Pages.BASE ).content.addSoundUpdateFunction($function);
		}
		
		
		/*********************************
		STOP LOOPS
		**********************************/
		public function stopLoops():void {
			Gaia.api.getPage( Pages.BASE ).content.stopAllLoops();
		}
		
		/*********************************
		START LOOPS
		**********************************/
		public function startLoops():void {
			Gaia.api.getPage( Pages.BASE ).content.startAllLoops();
		}
		
		/*********************************
		ADD SCORE
		**********************************/
		public function addScore($score:int):void {
			Gaia.api.getPage( Pages.BASE ).content.addToScore($score);
		}
		
		/*********************************
		SCORE UPDATE
		**********************************/
		public function addScoreUpdate($function:Function):void {
			Gaia.api.getPage( Pages.BASE ).content.addScoreUpdateFunction($function);
		}
		
		public function get score():int {
			return Gaia.api.getPage( Pages.BASE ).content.totalScore;
		}
		
		/*********************************
		TIMER
		**********************************/
		public function createTimer($time:int):void {
			Gaia.api.getPage( Pages.BASE ).content.createATimer($time);
		}
		
		public function get time():int {
			return Gaia.api.getPage( Pages.BASE ).content.timeLeft;
		}
		
		public function addTimeUpdate($function:Function):void {
			Gaia.api.getPage( Pages.BASE ).content.addTimeUpdateFunction($function);
		}
		
		public function stopTimer():int {
			return Gaia.api.getPage( Pages.BASE ).content.stopATimer();
		}
		
		public function startTimer($function:Function=null):int {
			return Gaia.api.getPage( Pages.BASE ).content.startATimer($function);
		}
		
		/*********************************
		TRACK
		**********************************/
		public function track($type:String):void {
			Gaia.api.getPage( Pages.BASE ).content.googleTrack($type);
		}
		
		/*********************************
		Create From String
		**********************************/
		public function createFromString($string:String):MovieClip {
			return new (getDefinitionByName($string))();
		}
		
		/*********************************
		COPY UPDATE
		**********************************/
		public function addCopyUpdate($function:Function):void {
			Gaia.api.getPage( Pages.BASE ).content.addCopyUpdateFunction($function);
		}
		
		public function copyUpdater($string:String, $type:String=null):void {
			Gaia.api.getPage( Pages.BASE ).content.updateCopy($string, $type);
		}
		
		/*********************************
		SET ORDER
		**********************************/
		public function setOrder( ...$functions):void {
			_current = 0;
			_functions = $functions;
			this.addEventListener(Event.COMPLETE, nextFunction);
			
			_functions[_current]();
		}
		
		private function nextFunction(e:Event):void {
			_current++;
			
			if (_current<_functions.length) _functions[_current]();
		}
		
		public function partDone(e:Event):void {
			this.dispatchEvent(e);
		}
	}
	
}
